package com.itheima.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * 延迟队列特性
 */
@Component
public class OrderListener implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //1.接收转换消息
            System.out.println(new String(message.getBody()));

            //2.处理业务逻辑
            System.out.println("业务逻辑的处理");
            System.out.println("根据订单id查询状态");
            System.out.println("判断状态是否支付成功");
            System.out.println("取消订单回滚库存");
            //3.手动签收
            channel.basicAck(deliveryTag,true);//签收成功
        } catch (Exception e) {
            // 4.e.printStackTrace(); 出现异常 --》拒绝签收
              /*
            第三个参数：requeue：重回队列。如果设置为true，则消息重新回到queue，broker会重新发送该消息给消费端
             */
            channel.basicNack(deliveryTag,true,false);
        }
    }
}
